<template>
  <el-dialog
    :visible.sync="visible"
    :before-close="beforeClose"
    :close-on-click-modal="false"
    class="dialog-sm"
    title="担保机构"
    top="30px"
  >
    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="creditAgencies"
      height="76vh"
    >
      <template v-slot:agencyName="scope">
        <div v-if="scope.row.access_agency_id === clientId">
          {{ scope.row.service_agency_name }}
        </div>
        <div v-else>{{ scope.row.agency_name }}</div>
      </template>
      <template v-slot:address="scope">
        <div
          class="text-truncate"
          v-if="scope.row.access_agency_id === clientId"
          :title="scope.row.service_agency_address"
        >
          {{ scope.row.service_agency_address }}
        </div>
        <div class="text-truncate" v-else :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:mobileNo="scope">
        <div v-if="scope.row.access_agency_id === clientId">
          {{ scope.row.service_agency_mobile }}
        </div>
        <div v-else>{{ scope.row.mobile_no }}</div>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="creditAgencyTotal"
        @current-change="getData"
      ></el-pagination>
      <div></div>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  components: {},
  props: {
    visible: Boolean,
  },
  data() {
    return {
      loading: false,
      params: {
        index: 1,
        size: 50,
        'agency_type.eq': 6,
        'access_status.in': '1,3',
      },
      tableName: 'credit_agency_access_view',
      creditAgencies: [],
      tableColumns: [
        { attrs: { label: '机构名称', prop: 'agency_name', align: 'center' }, slot: 'agencyName' },
        { attrs: { label: '机构地址', prop: 'address', align: 'center' }, slot: 'address' },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' }, slot: 'mobileNo' },
      ],
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId', 'creditAgencyTotal']),
  },
  watch: {},
  created() {
    this.getParams()
    this.getData()
  },
  mounted() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    getParams() {
      this.params['orWhere.access_agency_id.eq'] = this.clientId

      this.params['orWhere.service_id.eq'] = this.clientId
    },
    async getData() {
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params)
      )
      this.loading = false
      const data = (res && res.data) || {}

      // 担保公司的联盟成员，要排除自己
      const resData = data.data || []
      this.creditAgencies = resData.filter(item => item.client_id !== this.clientId)
      this.$store.commit(
        'creditAgencyTotal',
        (data.totalCount || 0) - (resData.length === this.creditAgencies.length ? 0 : 1)
      )
    },
  },
}
</script>
<style lang="scss" scoped></style>
